package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallAddressService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * litemall用户控制器
 *
 * @author yliyun
 * &#064;date  2022/09/19
 */
@Api(tags = "Litemall用户API",value = "Litemall用户控制器")
@RestController
@RequestMapping(value = {"/wx/litemallUser"})
@RequiredArgsConstructor
public class LitemallUserController extends ResponseUtil{

    /**
     * litemall用户服务
     */
    private final LitemallUserService litemallUserService;

    /**
     * wx用户服务
     */
    private final WxUserService wxUserService;


    /**
     * 按主键删除
     * TODO 用户注销-[物理删除]
     *
     * @param id id
     * @return {@link Response}<{@link Object}>
     */
    @LogAnno
    @ApiOperation("用户注销-[逻辑注销]")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "用户ID")
    })
    @GetMapping(value = {"/deleteByPrimaryKey"})
    public Response<Object> deleteByPrimaryKey(@ApiIgnore @LoginUser Integer userId, @RequestParam(name = "id") @NotNull Integer id){
        if(!userId.equals(id)){
            return ResponseUtil.fail(111,"用户注销失败,注销ID与登录用户不符！");
        }
        LitemallUser litemallUser = litemallUserService.findById(id);
        litemallUser.setStatus((byte) 3);
        litemallUser.setDeletedBy(userId);
//        litemallUser.setDeleted(true);
        litemallUser.setUpdateTime(LocalDateTime.now());
        /*if(litemallUser.getAvailableAmount().compareTo(new BigDecimal(5.00))==1){
            return ResponseUtil.fail(112,"当前账户余额大于5$");
        }*/
        return litemallUserService.updateById(litemallUser)?
                ResponseUtil.ok("用户注销成功,30天内登陆可自动解禁"):
                ResponseUtil.fail(111,"用户注销失败");
    }


    /**
     * APP奖励规则显示
     *
     * @param page        页面
     * @param limit       限制
     * @return {@link Object}
     */
    @ApiOperation("APP奖励规则显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isExpired",value = "是过期",defaultValue = "1"),
            @ApiImplicitParam(name = "page",value = "页面",defaultValue = "1"),
            @ApiImplicitParam(name = "limit",value = "限制",defaultValue = "10"),
    })
    @GetMapping("log")
    @LogAnno
    public Object log(@ApiIgnore @LoginUser Integer userId,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit){
        return wxUserService.log(page,limit,userId);
    }


    /**
     * 奖励规则列表
     *
     * @param isExpired 是过期
     * @param ruleName  规则名称
     * @param status    状态
     * @param page      页面
     * @param limit     限制
     * @param sort      排序
     * @param order     订单
     * @return {@link Object}
     */
    @ApiOperation("奖励规则列表")
    @GetMapping("/listRule")
    @LogAnno
    public Object listRule(Byte isExpired,String ruleName,Byte status,@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit,
                           @Sort @RequestParam(defaultValue = "add_time") String sort,
                           @Order @RequestParam(defaultValue = "desc") String order){
        return wxUserService.listRule(isExpired, page, limit, sort, order,ruleName,status);
    }


}
